Skip to content

Commit 22bd7a7

Browse files
authored
Merge pull request #392 from luis-fors-cb/specify-files-maven
Specify files maven
2 parents 0951220 + 138c525 commit 22bd7a7

File tree

5 files changed

+112
-2
lines changed

5 files changed

+112
-2
lines changed

plugin-maven/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### Version 1.22.0-SNAPSHOT - TBD ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-maven-plugin/snapshot/), [snapshot](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/spotless-maven-plugin/))
44

55
* Updated default eclipse-cdt from 4.7.3a to 4.11.0 ([#390](https://github.com/diffplug/spotless/pull/390)).
6+
* Added `-DspotlessFiles` switch to allow targeting specific files ([#392](https://github.com/diffplug/spotless/pull/392))
67

78
### Version 1.21.1 - March 29th 2019 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-maven-plugin/1.21.1/), [jcenter](https://bintray.com/diffplug/opensource/spotless-maven-plugin/1.21.1))
89

plugin-maven/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,16 @@ By default, `spotless:check` is bound to the `verify` phase. You might want to
377377

378378
<a name="examples"></a>
379379

380+
## Can I apply Spotless to specific files?
381+
382+
You can target specific files by setting the `spotlessFiles` project property to a comma-separated list of file patterns:
383+
384+
```
385+
cmd> mvn spotless:apply -DspotlessFiles=my/file/pattern.java,more/generic/.*-pattern.java
386+
```
387+
388+
The patterns are matched using `String#matches(String)` against the absolute file path.
389+
380390
## Example configurations (from real-world projects)
381391

382392
- [Apache Avro](https://github.com/apache/avro/blob/8026c8ffe4ef67ab419dba73910636bf2c1a691c/lang/java/pom.xml#L307-L334)

plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import java.io.File;
2121
import java.io.IOException;
2222
import java.util.*;
23+
import java.util.function.Predicate;
24+
import java.util.regex.Pattern;
25+
import java.util.stream.Collectors;
2326
import java.util.stream.Stream;
2427

2528
import org.apache.maven.plugin.AbstractMojo;
@@ -100,6 +103,9 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo {
100103
@Deprecated
101104
private com.diffplug.spotless.maven.css.Css css;
102105

106+
@Parameter(property = "spotlessFiles")
107+
private String filePatterns;
108+
103109
protected abstract void process(List<File> files, Formatter formatter) throws MojoExecutionException;
104110

105111
@Override
@@ -132,7 +138,22 @@ private List<File> collectFiles(FormatterFactory formatterFactory) throws MojoEx
132138
String excludesString = String.join(",", excludes);
133139

134140
try {
135-
return FileUtils.getFiles(baseDir, includesString, excludesString);
141+
final List<File> files = FileUtils.getFiles(baseDir, includesString, excludesString);
142+
if (filePatterns == null || filePatterns.isEmpty()) {
143+
return files;
144+
}
145+
final String[] includePatterns = this.filePatterns.split(",");
146+
final List<Pattern> compiledIncludePatterns = Arrays.stream(includePatterns)
147+
.map(Pattern::compile)
148+
.collect(Collectors.toList());
149+
final Predicate<File> shouldInclude = file -> compiledIncludePatterns
150+
.stream()
151+
.anyMatch(filePattern -> filePattern.matcher(file.getAbsolutePath())
152+
.matches());
153+
return files
154+
.stream()
155+
.filter(shouldInclude)
156+
.collect(toList());
136157
} catch (IOException e) {
137158
throw new MojoExecutionException("Unable to scan file tree rooted at " + baseDir, e);
138159
}

plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public File locateFile(String path) {
4646
} catch (ResourceNotFoundException e) {
4747
throw new RuntimeException("Unable to locate file with path: " + path, e);
4848
} catch (FileResourceCreationException e) {
49-
throw new RuntimeException("Unable to create temporaty file '" + outputFile + "' in the output directory", e);
49+
throw new RuntimeException("Unable to create temporary file '" + outputFile + "' in the output directory", e);
5050
}
5151
}
5252

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2016 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.maven;
17+
18+
import java.io.IOException;
19+
20+
import org.junit.Test;
21+
22+
public class SpecificFilesTest extends MavenIntegrationTest {
23+
private String testFile(int number, boolean absolute) throws IOException {
24+
String rel = "src/main/java/test" + number + ".java";
25+
if (absolute) {
26+
return rootFolder() + "/" + rel;
27+
} else {
28+
return rel;
29+
}
30+
}
31+
32+
private String testFile(int number) throws IOException {
33+
return testFile(number, false);
34+
}
35+
36+
private String fixture(boolean formatted) {
37+
return "java/googlejavaformat/JavaCode" + (formatted ? "F" : "Unf") + "ormatted.test";
38+
}
39+
40+
private void integration(String patterns, boolean firstFormatted, boolean secondFormatted, boolean thirdFormatted)
41+
throws IOException, InterruptedException {
42+
43+
writePomWithJavaSteps(
44+
"<includes>",
45+
" <include>src/**/java/**/*.java</include>",
46+
"</includes>",
47+
"<googleJavaFormat>",
48+
" <version>1.2</version>",
49+
"</googleJavaFormat>");
50+
51+
setFile(testFile(1)).toResource(fixture(false));
52+
setFile(testFile(2)).toResource(fixture(false));
53+
setFile(testFile(3)).toResource(fixture(false));
54+
55+
mavenRunner()
56+
.withArguments("spotless:apply", "-DspotlessFiles=" + patterns)
57+
.runNoError();
58+
59+
assertFile(testFile(1)).sameAsResource(fixture(firstFormatted));
60+
assertFile(testFile(2)).sameAsResource(fixture(secondFormatted));
61+
assertFile(testFile(3)).sameAsResource(fixture(thirdFormatted));
62+
}
63+
64+
@Test
65+
public void singleFile() throws IOException, InterruptedException {
66+
integration(testFile(2, true), false, true, false);
67+
}
68+
69+
@Test
70+
public void multiFile() throws IOException, InterruptedException {
71+
integration(testFile(1, true) + "," + testFile(3, true), true, false, true);
72+
}
73+
74+
@Test
75+
public void regexp() throws IOException, InterruptedException {
76+
integration(".*/src/main/java/test\\(1\\|3\\).java", true, false, true);
77+
}
78+
}

0 commit comments

Comments
 (0)